<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class FormCreator
{
	public function create($name, $contents, $action = "", $method = "POST", $buttons = "", $encoding = "") {
		global $CONFIG;
		if (!$buttons) {
			$buttons[] = $this->button("submit", "submitbutton", $CONFIG['button_submit'], 0, "");
		}

		$contents[] = $this->input_hidden("submitted", 1, 1);
		return array('name' => $name, 'contents' => $contents, 'action' => ($action ? $action : $_SERVER['REQUEST_URI']), 'method' => $method, 
				 'buttons' => $buttons, 'encoding' => $encoding);
	}

	public function header($title) {
		return array('type' => 'header', 'title' => $title);
	}

	public function info($info, $hidden = 0, $unhidetext = 'info', $title = "") {
		return array('type' => 'info', 'info' => $info, 'hidden' => $hidden, 'title' => $title, 'unhidetext' => $unhidetext);
	}

	public function input_field($title, $name, $value = "", $size = 0, $maxlength = "", $onchange = "", $readonly = 0, $password = 0, $locked_default = 0) {
		if ($_POST[$name] and !$locked_default) {
			$value = stripslashes($_POST[$name]);
		}
		return array('title' => $title, 'type' => "field", 'password' => $password, 'name' => $name, 'value' => $value, 'size' => $size, 
				 'maxlength' => $maxlength, 'onchange' => $onchange, 'readonly' => $readonly);
	}

	public function input_hidden($name, $value, $locked_default = 0) {
		if ($_POST[$name] and !$locked_default) {
			$value = $_POST[$name];
		}
		return array('type' => "hidden", 'name' => $name, 'value' => $value);
	}

	public function input_checkbox($title, $name, $checked = "", $onchange = "", $readonly = 0, $locked_default = 0) {
		if ($_POST[$name] and !$locked_default) {
			$checked = "checked";
		}
		return array('title' => $title, 'type' => "checkbox", 'name' => $name, 
					 'checked' => $checked, 'onchange' => $onchange, 'readonly' => $readonly);
	}

	public function input_radio($title, $name, $keys, $values, $default = "", $onchange = "", $readonly = 0, $locked_default = 0) {
		if ($_POST[$name] and !$locked_default) {
			$default = $_POST[$name];
		}
		return array('title' => $title, 'type' => "radio", 'name' => $name, 
					 'keys' => $keys, 'values' => $values, 'default' => $default, 
					 'onchange' => $onchange, 'readonly' => $readonly);
	}

	public function input_file($title, $name, $value = "", $size = 0, $onchange = "", $readonly = 0, $locked_default = 0) {
		if ($_POST[$name] and !$locked_default) {
			$value = $_POST[$name];
		}
		return array('title' => $title, 'type' => "file", 'name' => $name, 
					 'value' => $value, 'size' => $size, 'onchange' => $onchange, 
					 'readonly' => $readonly);
	}

	public function input_select($title, $name, $options, $values, $topvalue = "", $default = "", $onchange = "", $size = 1, $readonly = 0, $locked_default = 0) {
		if ($_POST[$name] and !$locked_default) {
			$default = $_POST[$name];
		}
		return array('title' => $title, 'type' => "select", 'name' => $name,
					 'options' => $options, 'values' => $values, 'topvalue' => $topvalue, 
					 'default' => $default, 'onchange' => $onchange, 'size' => $size, 'readonly' => $readonly);
	}

	public function input_text($title, $name, $value = "", $rows = 0, $cols = 0, 
			$onchange = "", $readonly = 0, $locked_default = 0) {

		if ($_POST[$name] and !$locked_default) {
			$value = stripslashes($_POST[$name]);
		}
		return array('title' => $title, 'type' => "text", 'name' => $name,
					 'value' => $value, 'rows' => $rows, 'cols' => $cols, 'onchange' => $onchange, 
					 'readonly' => $readonly);
	}

	public function input_date($title, $name, $default = array(), $onchange = "", $readonly = 0, $locked_default = 0) {
		if (($_POST[$name.'_day'] or $_POST[$name.'_month'] or $_POST[$name.'_year']) and !$locked_default)
			$default = array('day' => $_POST[$name.'_day'], 'month' => $_POST[$name.'_month'], 'year' => $_POST[$name.'_year']);
		return array('title' => $title, 'type' => "date", 'name' => $name,
					 'default' => $default, 'onchange' => $onchange, 'readonly' => $readonly);
	}

	public function button($type, $name, $value, $size = "", $onclick = "") {
		return array('name' => $name, 'type' => $type, 'value' => $value, 
					 'size' => $size, 'onclick' => $onclick);
	}

	public function confirm($string) {
		$fields = array();
		$keys = array_keys($_POST);
		$vals = array_values($_POST);
		while ($k = array_pop($keys)) {
			$v = array_pop($vals);
			$fields[] = $this->input_hidden($k, $v);
		}
		$fields[] = $this->input_radio($string, 'confirmed', array('YES', 'NO'), array('ja', 'nee'));
		return $this->create('confirmationform', $fields);
	}

	public function input_hiddencomments($id) {
		$txt = "<textarea rows=\"4\" cols=\"75\" name=\"$id\"> </textarea>";
		$creator = new FormCreator();
		return $creator->info($txt, 1, "<span>commentaar</span>");
	}

	public function input_nonhiddencomments($id, $comments) {
		$txt = "<textarea rows=\"4\" cols=\"75\" name=\"$id\">$comments</textarea>";
		$creator = new FormCreator();
		return $creator->info($txt);
	}

}
?>
